//
//  HMCarousel.m
//  HMDemo
//
//  Created by mengxiangyu on 2017/7/12.
//  Copyright © 2017年 mengxiangyu. All rights reserved.
//

#import "HMCarousel.h"
#import "HMCarouselScrollView.h"

@interface HMCarousel ()<UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (assign, nonatomic) NSInteger index;
@property (strong, nonatomic) UICollectionView *collectionView;
@property (assign, nonatomic) BOOL repeat;
@end

@implementation HMCarousel

static NSString *carouselIdentifier = @"carousel";

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)showCarouselRect:(CGRect)carouselRect carouselCount:(NSInteger)carouselCount target:(id)target repeat:(BOOL)repeat {
    
    _repeat = repeat;
    _carouselCount = carouselCount;
    self.delegate = target;
    
    if (repeat) { // 轮播
        _scrollRed = [[HMCarouselScrollView alloc] initWithFrame:carouselRect];
        _scrollRed.contentOffset = CGPointMake(_scrollRed.frame.size.width, 0);
        _scrollRed.delegate = self;
        _scrollRed.tag = 0;
        [self addSubview:_scrollRed];
        
        _scrollBlue = [[HMCarouselScrollView alloc] initWithFrame:carouselRect];
        _scrollBlue.contentOffset = CGPointMake(_scrollBlue.frame.size.width, 0);
        _scrollBlue.delegate = self;
        _scrollBlue.tag = 1;
        [self addSubview:_scrollBlue];
        
        _currScrollView = _scrollBlue;
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchCarousel)];
        [_scrollRed addGestureRecognizer:tap];
        [_scrollBlue addGestureRecognizer:tap];
        
        [self setCarousel];
        
    } else { // 不轮播
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//        CGFloat mainWidth = [[UIScreen mainScreen] bounds].size.width;
//        CGFloat dayLabelWidth = (mainWidth-2*8)/7;
//        CGFloat dayLabelHeight = dayLabelWidth*0.9;
        //        self.headerReferenceSize = CGSizeMake(mainWidth, 65.0f);//头部视图的框架大小
        layout.itemSize = carouselRect.size;//每个cell的大小
        XYLog(@"%f---%f", carouselRect.size.width, carouselRect.size.height);
        layout.minimumLineSpacing = 0.0f;//每行的最小间距
        layout.minimumInteritemSpacing = 0.0f;//每列的最小间距
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//网格视图的/上/左/下/右,的边距
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        // 添加collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:carouselRect collectionViewLayout:layout];
        
        // 告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中)
        [self.collectionView registerClass:[HMCollectionViewItem class] forCellWithReuseIdentifier:carouselIdentifier];
        self.collectionView.backgroundColor = [UIColor clearColor];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:_collectionView];
    }
    
}

- (void)touchCarousel {
    NSLog(@"%ld", (_index+1)%_carouselCount);
    if ([self.delegate respondsToSelector:@selector(touchCarouselIndex:)]) {
        [self.delegate touchCarouselIndex:(_index+1)%_carouselCount];
    }
}


#pragma mark - 设置轮播视图数量
- (void)setCarouselCount:(NSInteger)carouselCount {
    _carouselCount = carouselCount;
    if (_repeat) {
        [self setCarousel];
    } else {
        [self.collectionView reloadData];
    }
}

#pragma mark - 无限轮播相关设置
- (void)setCarousel {
    
    NSInteger kZero;
    NSInteger kOne;
    NSInteger kTwo;
    
    if (_carouselCount >= 1) {
        
        _index = _carouselCount-1;
        kZero = _index % _carouselCount;
        kOne = (_index+1) % _carouselCount;
        kTwo = (_index+2) % _carouselCount;
        
        if ([self.delegate respondsToSelector:@selector(setContentView:index:)]) {
            [self.delegate setContentView:_currScrollView.firstView index:kZero];
            [self.delegate setContentView:_currScrollView.secondView index:kOne];
            [self.delegate setContentView:_currScrollView.thirdView index:kTwo];
            
        }
        
    } else {
        _index = 0;
        _scrollBlue.scrollEnabled = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(HMCarouselScrollView *)scrollView {
    
    if (_repeat) {
        NSInteger kZero;
        NSInteger kOne;
        NSInteger kTwo;
        
        if (scrollView.contentOffset.x==0) {
            
            if (_carouselCount == 0) {
                _index = 0;
                
                kOne = 0;
                
                scrollView.tag = 0;
                _currScrollView.tag = 1;
                
                [self insertSubview:scrollView belowSubview:_currScrollView];
                _currScrollView = scrollView;
                scrollView.contentOffset = CGPointMake(_currScrollView.frame.size.width, 0);
                
            } else {
                
                _index = _index - 1;
                
                if (_index < 0) {
                    _index = _carouselCount-1;
                }
                kZero = _index % _carouselCount;
                kOne = (_index+1) % _carouselCount;
                kTwo = (_index+2) % _carouselCount;
                
                NSLog(@"右滑==%ld---%ld---%ld, index=%ld", kZero, kOne, kTwo, _index);
                
                scrollView.tag = 0;
                
                _currScrollView.tag = 1;
                if ([self.delegate respondsToSelector:@selector(setContentView:index:)]) {
                    [self.delegate setContentView:_currScrollView.firstView index:kZero];
                    [self.delegate setContentView:_currScrollView.secondView index:kOne];
                    [self.delegate setContentView:_currScrollView.thirdView index:kTwo];
                }
                
                [self insertSubview:scrollView belowSubview:_currScrollView];
                _currScrollView = scrollView;
                scrollView.contentOffset = CGPointMake(_currScrollView.frame.size.width, 0);
                
            }
            
            
        } else if (scrollView.contentOffset.x==_currScrollView.frame.size.width*2) {
            
            if (_carouselCount == 0) {
                _index = 0;
                
                kOne = 0;
                
                scrollView.tag = 0;
                _currScrollView.tag = 1;
                
                [self insertSubview:scrollView belowSubview:_currScrollView];
                _currScrollView = scrollView;
                scrollView.contentOffset = CGPointMake(_currScrollView.frame.size.width, 0);
                
            } else {
                
                _index = _index + 1;
                
                if (_index < 0) {
                    _index = _carouselCount-1;
                }
                kZero = _index % _carouselCount;
                kOne = (_index+1) % _carouselCount;
                kTwo = (_index+2) % _carouselCount;
                
                NSLog(@"左滑==%ld---%ld---%ld, index=%ld", kZero, kOne, kTwo, _index);
                
                scrollView.tag = 0;
                _currScrollView.tag = 1;
                if ([self.delegate respondsToSelector:@selector(setContentView:index:)]) {
                    [self.delegate setContentView:_currScrollView.firstView index:kZero];
                    [self.delegate setContentView:_currScrollView.secondView index:kOne];
                    [self.delegate setContentView:_currScrollView.thirdView index:kTwo];
                }
                
                [self insertSubview:scrollView belowSubview:_currScrollView];
                _currScrollView = scrollView;
                scrollView.contentOffset = CGPointMake(_currScrollView.frame.size.width, 0);
                
            }
            
        }

        if ([self.delegate respondsToSelector:@selector(scrollToIndex:)]) {
            [self.delegate scrollToIndex:(_index+1)%_carouselCount];
        }

    } else {
        if ([self.delegate respondsToSelector:@selector(scrollToIndex:)]) {
            [self.delegate scrollToIndex:_collectionView.contentOffset.x/_collectionView.frame.size.width];
        }
    }
    
}

#pragma mark - 有限轮播相关设置
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _carouselCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HMCollectionViewItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:carouselIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor orangeColor];
    if ([self.delegate respondsToSelector:@selector(setItemView:index:)]) {
        [self.delegate setItemView:cell index:indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    if ([self.delegate respondsToSelector:@selector(touchCarouselIndex:)]) {
        [self.delegate touchCarouselIndex:indexPath.row];
    }
}


#pragma mark - 释放通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
